#####
## Replication File:
##
## Christian Nationalism and Political Violence: 
## Victimhood, White Identity, Conspiracy and 
## Support for the Capitol Attacks
##
## Miles T. Armaly, David T. Buckley, and Adam M. Enders
#####

################################################################################

####
## Load necessary packages
## 
## Analyses conducted in RStudio 1.0.153,
## using R 3.6 (Mac OS)
####

library(stats)
library(car)
library(lattice)
library(latticeExtra)
library(foreign)

# Set working directory

################################################################################

#####
## Figure 1
#####

febdata <- read.dta("Clean Data, Feb 2021.dta")

hist1 <- histogram(as.factor(febdata$justified),
                   aspect = 1,
                   main = "A. Capitol Riot Justified",
                   xlab = "",
                   scales = list(x = list(labels = c("Str. Disagree (1)", "Disagree (2)",
                                                     "Neither/Nor (3)", "Agree (4)", "Str. Agree (5)"),
                                          rot = 25))
                   )

hist2 <- histogram(febdata$violence,
                   aspect = 1,
                   main = "B. Support for Political Violence",
                   xlab = "",
                   scales = list(x = list(labels = c("Not Support (1)", 
                                                     "Neutral (3)", "Support (5)"),
                                          at = c(1, 3, 5), rot = 25))
)

tiff("figure1.tiff", width = 8, height = 4, units = "in", res=300, compression = 'lzw')
print(hist1, position = c(0, 0, .5, 1), more = TRUE)
print(hist2, position = c(.5, 0, 1, 1), more = FALSE)
dev.off()

################################################################################

####
## Figure 2
####

my.panel.bands <-
  function(x, y, upper, lower,
           fill, col,
           subscripts, ..., font, fontface)
  {
    upper <- upper[subscripts]
    lower <- lower[subscripts]
    panel.polygon(c(x, rev(x)), c(upper, rev(lower)),
                  col = fill, border = FALSE,
                  ...)
  }

pred1 <- read.dta("Justified Predictions.dta")

plot1 <- xyplot(estimate ~ scale | order,
       data = pred1,
       aspect = 1,
       groups = order,
       type = "l",
       ylim = c(0.75, 3.5),
       fill = c("light grey"),
       upper = pred1$upper,
       lower = pred1$lower,
       layout = c(4, 1),
       main = "A. Capitol Riot Justified",
       xlab = " ",
       ylab = "Predicted Attitude",
       strip=strip.custom(factor.levels=c("Christian Nationalism",
                                          "White Identity", 
                                          "Perceived Victimhood",
                                          "Support QAnon")),
       col = "black",
       panel = function(x, y, ...){
         panel.superpose(x, y, panel.groups = 'my.panel.bands', ...)
         panel.xyplot(x, y, ...)
       }
)

pred2 <- read.dta("Violence Predictions.dta")

plot2 <- xyplot(estimate ~ scale | order,
                data = pred2,
                aspect = 1,
                groups = order,
                type = "l",
                ylim = c(0.75, 3.3),
                fill = c("light grey"),
                upper = pred2$upper,
                lower = pred2$lower,
                layout = c(4, 1),
                main = "B. Acceptance of Violence",
                xlab = " ",
                ylab = "Predicted Attitude",
                strip=strip.custom(factor.levels=c("Christian Nationalism",
                                                   "White Identity", 
                                                   "Perceived Victimhood",
                                                   "Support QAnon")),
                col = "black",
                panel = function(x, y, ...){
                  panel.superpose(x, y, panel.groups = 'my.panel.bands', ...)
                  panel.xyplot(x, y, ...)
                }
)

tiff("figure2.tiff", width = 9, height = 8, units = "in", res=300, compression = 'lzw')
print(plot1, position = c(0, .5, 1, 1), more = TRUE)
print(plot2, position = c(0, 0, 1, .5), more = FALSE)
dev.off()

################################################################################

####
## Figure 3
####

mfx1 <- read.dta("Justified Marginal Effects.dta")

plot3 <- xyplot(estimate ~ scale | order,
       data = mfx1,
       aspect = 1,
       groups = order,
       type = "l",
       ylim = c(-2.2, 4.5),
       fill = c("light grey"),
       upper = mfx1$upper,
       lower = mfx1$lower,
       main = "A. Capitol Riot Justified",
       xlab = " ",
       ylab = "M.E. of Christian Nationalism",
       layout = c(3, 1),
       strip=strip.custom(factor.levels=c("White Identity", 
                                          "Perceived Victimhood",
                                          "Support QAnon")),
       col = "black",
       panel = function(x, y, ...){
         panel.superpose(x, y, panel.groups = 'my.panel.bands', ...)
         panel.xyplot(x, y, ...)
         panel.abline(h=0, lty=2)
       }
)


mfx2 <- read.dta("Violence Marginal Effects.dta")

plot4 <- xyplot(estimate ~ scale | order,
       data = mfx2,
       aspect = 1,
       groups = order,
       type = "l",
       ylim = c(-1.2, 3.7),
       fill = c("light grey"),
       upper = mfx2$upper,
       lower = mfx2$lower,
       main = "B. Acceptance of Violence",
       xlab = " ",
       ylab = "M.E. of Christian Nationalism",
       layout = c(3, 1),
       strip=strip.custom(factor.levels=c("White Identity", 
                                          "Perceived Victimhood",
                                          "Support QAnon")),
       col = "black",
       panel = function(x, y, ...){
         panel.superpose(x, y, panel.groups = 'my.panel.bands', ...)
         panel.xyplot(x, y, ...)
         panel.abline(h=0, lty=2)
       }
)

tiff("figure3.tiff", width = 8, height = 8, units = "in", res=300, compression = 'lzw')
print(plot3, position = c(0, .5, 1, 1), more = TRUE)
print(plot4, position = c(0, 0, 1, .5), more = FALSE)
dev.off()

################################################################################

####
## Figure A1
####

tiff("figureA1.tiff", width = 5, height = 5, units = "in", res=300, compression = 'lzw')
histogram(febdata$nationalism,
          aspect = 1,
          #main = "A. Christian Nationalism",
          xlab = "",
          scales = list(x = list(labels = c("Low (1)", "Neutral (3)",
                                            "High (5)"),
                                 at = c(1, 3, 5)))
)
dev.off()

################################################################################

####
## Figure A2
####

# Panel A
means <- read.csv("CN by Affiliation.csv", header = TRUE)

means$order <- reorder(means$religion, means$mean)

n <- length(means$religion)

dot1 <- dotplot(order ~ mean,
                data = means,
                aspect = 1.25,
                xlab = "Mean of Christian Nationalism",
                ylab = "",
                main = "A. Religious Affiliation",
                col = "black",
                pch = 16,
                xlim = c(1.2, 4.5),
                panel = function (x, y) {
                  panel.segments(rep(-0.7, n), as.numeric(means$order),
                                 x, as.numeric(means$order), 
                                 lty = 2, col = "gray")
                  panel.xyplot(means$mean, as.numeric(means$order),
                               pch = 16, col = "black")
                  panel.segments(means$lower, as.numeric(means$order),
                                 means$upper, as.numeric(means$order), 
                                 lty = 1, col = "black")
                }
)

# Panel B
means2 <- read.csv("CN by Political Orientation.csv", header = TRUE)

means2$order <- reorder(means2$pol, means2$mean)

n <- length(means2$pol)

dot2 <- dotplot(order ~ mean,
                data = means2,
                aspect = 1.25,
                xlab = "Mean of Christian Nationalism",
                ylab = "",
                main = "B. Political Orientation",
                col = "black",
                pch = 16,
                xlim = c(1.2, 4.5),
                panel = function (x, y) {
                  panel.segments(rep(-0.7, n), as.numeric(means2$order),
                                 x, as.numeric(means2$order), 
                                 lty = 2, col = "gray")
                  panel.xyplot(means2$mean, as.numeric(means2$order),
                               pch = 16, col = "black")
                  panel.segments(means2$lower, as.numeric(means2$order),
                                 means2$upper, as.numeric(means2$order), 
                                 lty = 1, col = "black")
                }
)

tiff("figureA2.tiff", width = 8, height = 5, units = "in", res=300, compression = 'lzw')
print(dot1, position = c(0, 0, .5, 1), more = TRUE)
print(dot2, position = c(.5, 0, 1, 1), more = FALSE)
dev.off()


